﻿using DeHeng_Mes.Dao;
using DeHeng_Mes.Model;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Model.ViewModel;
using DeHeng_Mes.Utils;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Service
{
    public static class ViewService
    {
        static ViewDao viewDao = new ViewDao();

        static OrderDao orderDao = new OrderDao();

        /// <summary>
        /// 工艺数据推送通用服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="traceId"></param>
        /// <param name="station"></param>
        /// <param name="datas"></param>
        public static void InsertLineMesData<T>(string traceId, string station, List<T> datas) where T : class
        {
            try
            {
                //查找id查找对应推送的工单
                OrderTrace orderTrace = orderDao.GetOrderCreatetime(traceId);

                //获取工序号
                string operationId = MappingTable.GetOperationId(station);

                if (orderTrace == null || datas == null || datas.Count <= 0)
                    return;

                //拿到唯一标识
                string code = Tools.GetHashCodeFromPushMes(orderTrace.OrderId, orderTrace.TraceId, orderTrace.CreateTime);

                List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();

                foreach (var data in datas)
                {
                    //将拉铆工艺数据转成dic
                    List<KeyValuePair<string, string>> list = data.ToPropertyDictionary();

                    keyValuePairs.AddRange(list);
                }

                List<LineMesDataTraceTech> lineMesDataTraceTechs = new List<LineMesDataTraceTech>();

                DateTime now = DateTime.Now;

                foreach (var item in keyValuePairs)
                {
                    if (string.IsNullOrEmpty(item.Value) || item.Key.Equals("Id"))
                        continue;

                    LineMesDataTraceTech lineMesDataTraceTech = new LineMesDataTraceTech()
                    {
                        CreateTime = now,
                        DataFrom = "DEH-W03",
                        DataId = code,
                        DataTraceTechId = Guid.NewGuid().ToString(),
                        Process = operationId,
                        SubEqptId = station,
                        UserId = "",
                        VarName = item.Key,
                        Value = item.Value,
                    };

                    lineMesDataTraceTechs.Add(lineMesDataTraceTech);
                }

                if (lineMesDataTraceTechs.Count > 0)
                {
                    int count = viewDao.PushOperationData(lineMesDataTraceTechs);

                    Tools.ProduceLogText($"[{station}]推送了[{count}]条数据，组装条目[{keyValuePairs.Count}]条数据，追溯码为:[{traceId}]");
                }
            }
            catch (Exception e)
            {
                Tools.ProduceLogTextError($"[{station}]推送数据失败，错误信息：{e.Message}");
            }
        }
    }
}
